package com.fortune.rainshineservice.service.impl;

import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.entity.AreaEntity;
import com.fortune.rainshinecommon.entity.CbmCityEntity;
import com.fortune.rainshinecommon.entity.CityEntity;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.CbmCityModel;
import com.fortune.rainshineservice.mapper.CbmCityMapper;
import com.fortune.rainshineservice.mapper.ProvinceMapper;
import com.fortune.rainshineservice.service.ICbmCityService;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @program: rainShine
 * @description:
 * @author: tangyn
 * @create: 2019-05-20
 **/
@Service
public class CbmCityServiceImpl implements ICbmCityService {
    private static final Logger logger = LoggerFactory.getLogger(CbmCityServiceImpl.class);

    @Autowired
    private CbmCityMapper cbmCityMapper;

    @Autowired
    private ProvinceMapper provinceMapper;


    @Override
    public MsgModel getCbmCityList() {
        try{
            List<CbmCityModel> allProvinces = Optional.ofNullable(cbmCityMapper.getProvinces()).orElse(Lists.newArrayList());
            List<CbmCityEntity> cbmCityEntities = Lists.newArrayList();
            if(!allProvinces.isEmpty()){
                allProvinces.forEach(e->{
                    CbmCityEntity cbmCityEntity = new CbmCityEntity();
                    cbmCityEntity.setProvinceCode(e.getCityCode());
                    cbmCityEntity.setProvinceName(e.getCityName());
                    List<CbmCityModel> cities = Optional.ofNullable(cbmCityMapper.getCitiesByProvince(e.getId())).orElse(Lists.newArrayList());
                    List<CityEntity> cityEntities = Lists.newArrayList();
                    if(!cities.isEmpty()){
                        cities.forEach(f->{
                            CityEntity cityEntity = new CityEntity();
                            cityEntity.setCityCode(f.getCityCode());
                            cityEntity.setCityName(f.getCityName());
                            List<CbmCityModel> areas = Optional.ofNullable(cbmCityMapper.getAreaByCityCode(f.getId())).orElse(Lists.newArrayList());
                            List<AreaEntity> areaEntities = Lists.newArrayList();
                            if(!areas.isEmpty()){
                                areas.forEach(g->{
                                    AreaEntity areaEntity = new AreaEntity();
                                    areaEntity.setAreaCode(g.getCityCode());
                                    areaEntity.setAreaName(g.getCityName());
                                    areaEntities.add(areaEntity);
                                });
                            }
                            cityEntity.setAreas(areaEntities);
                            cityEntities.add(cityEntity);
                        });
                    }
                    cbmCityEntity.setCities(cityEntities);
                    cbmCityEntities.add(cbmCityEntity);
                });
            }
            return new MsgModel(ResultCode.SUCCESS, cbmCityEntities);
        } catch (Exception e){
            logger.error("getCbmCityList ==> {}", e);
            return new MsgModel(ResultCode.FAILURE);
        }
    }

    @Override
    public String getCityNameByCode(String cityCode) {
        return cbmCityMapper.getCityNameByCode(cityCode);
    }

    @Override
    public String getProvinceNameByCode(String provinceCode) {
        return cbmCityMapper.getProvinceNameByCode(provinceCode);
    }
}
